
package kuti.grp5;

import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.Map;
import kuti.IWorldElement;

/**
 * 
 * The collision list contains all collision occured for the current kuti, and 
 * for one step. If a second collision occured and the distance between the kuti
 * and the second collision is smaller that the distance between the kuti and
 * the first collision, the first collision is deleted.
 * 
 * @author Marc Schorderet <marc.schorderet@edu.hefr.ch>
 * @author Vincenzo Do <vincenzo.do@edu.hefr.ch>
 *
 */
public class CollisionList {
    
    /**
     * Map of all collision occured for one kuti on one step
     */
    
    private Map<IWorldElement, Point2D> collisions;
    
    /**
     * Distance between the kuti and the collisions
     */
    
    double distance;
    
    /**
     * Creates a empty map to stock the collisions.
     */
    
    public CollisionList(){
        collisions = new HashMap();
        distance = Double.MAX_VALUE;
    }
    
    /**
     * Adds a new collsion. This Map contains juste the smallest distance
     * between the collision point and the kuti. If two collision have the same
     * distance, the two collisions are stocked in the map.
     * 
     * @param element           The element with which collides kuti.
     * @param collisionPoint    The collsion point of the kuti
     * @param distance          The distance between the kuti center and the collision point
     * 
     */
    
    public void addNewCollision(IWorldElement element, Point2D collisionPoint, double distance){
        
        if(element == null){
            throw new IllegalArgumentException("The element is not defined.");
        }
        
        if(collisionPoint == null){
            throw new IllegalArgumentException("The collision point is not defined.");
        }
        
        if(Double.isNaN(collisionPoint.getX()) || Double.isNaN(collisionPoint.getY())){
            throw new IllegalArgumentException("The collision point values are not a number");
        }
        
        if(Double.isNaN(distance)){
            throw new IllegalArgumentException("The distance is not a number");
        }
        
        if(distance < this.distance){
            collisions.clear();
            collisions.put(element, collisionPoint);
            this.distance = distance;
        }else if(distance == this.distance){
            collisions.put(element, collisionPoint);
        }
        
    }
    
    /**
     * Get the collsions map
     * 
     * @return Returns collision map.
     * 
     */
    
    public Map<IWorldElement, Point2D> getCollisions(){
        return collisions;
    }
    
    /**
     * Get the collsion distance
     * 
     * @return Returns the collision distance. 
     */
    
    public double getDistance(){
        return distance;
    }
    
    /**
     * Check if kuti has a collision.
     * 
     * @return Returns false if the map is empty, else returns true.
     */
    
    public boolean hasACollision(){
        return !collisions.isEmpty();
    }
    
    
}
